// Tradex.Connectivity.Rithmic.h

#pragma once

#include "vcclr.h"
#include "RApi.h"
#include "RithmicConnector.h"
#include "ConnectionSetup.h"

using namespace System;
using namespace System::Data::Common;
using namespace System::IO;
using namespace System::Reflection;
using namespace RApi;

namespace Tradex {
	namespace Connectivity {
		namespace Rithmic {

			ref class RithmicConnector;

			public class NativeConnector : public AdmCallbacks, public RCallbacks
			{
				public :
				NativeConnector (RithmicConnector^ pManagedConnector);
				~NativeConnector ();

				void Connect (REngineParams* pEngineParams, LoginParams* pLoginParams);
				void Disconnect ();

				/*   ----------------------------------------------------------------   */

				Tradex::Trading::OrderUpdate^ NativeConnector::ProcessLineInfo (LineInfo *pLineInfo, bool current);

				/*   ----------------------------------------------------------------   */

				virtual int Alert(AlertInfo *pInfo, void *pContext, int *aiCode);

				virtual int AskQuote(AskInfo *pInfo, void *pContext, int *aiCode);

				virtual int BestAskQuote(AskInfo *pInfo, void *pContext, int *aiCode);

				virtual int BestBidQuote(BidInfo *pInfo, void *pContext, int *aiCode);

				virtual int BidQuote(BidInfo *pInfo, void *pContext, int *aiCode);

				virtual int ClosePrice(ClosePriceInfo *pInfo, void *pContext, int * aiCode);

				virtual int ClosingIndicator(ClosingIndicatorInfo *pInfo, void *pContext, int *aiCode);

				virtual int HighPrice(HighPriceInfo *pInfo, void *pContext, int *aiCode);

				virtual int LimitOrderBook(LimitOrderBookInfo *pInfo, void *pContext, int *aiCode);

				virtual int LowPrice(LowPriceInfo *pInfo, void *pContext, int *aiCode);

				virtual int MarketMode(MarketModeInfo * pInfo, void *pContext, int *aiCode);

				virtual int OpenPrice(OpenPriceInfo *pInfo, void *pContext, int *aiCode);

				virtual int OpeningIndicator(OpeningIndicatorInfo * pInfo, void *pContext, int *aiCode);

				virtual int RefData(RefDataInfo *pInfo, void *pContext, int *aiCode);

				virtual int SettlementPrice(SettlementPriceInfo *pInfo, void *pContext, int *aiCode);

				virtual int TradeCondition(RApi::TradeInfo *pInfo, void *pContext, int *aiCode);

				virtual int TradePrint(RApi::TradeInfo *pInfo, void *pContext, int *aiCode);

				virtual int TradeReplay(TradeReplayInfo * pInfo, void *pContext, int *aiCode);

				virtual int TradeVolume(TradeVolumeInfo *pInfo, void *pContext, int *aiCode);

				/*   ----------------------------------------------------------------   */

				virtual int AccountList(AccountListInfo *pInfo, void *pContext, int *aiCode);

				/*   ----------------------------------------------------------------   */

				virtual int ExecutionReplay(ExecutionReplayInfo *pInfo, void *pContext, int *aiCode);

				virtual int LineUpdate(LineInfo *pInfo, void *pContext, int *aiCode);

				virtual int OpenOrderReplay(OrderReplayInfo *pInfo, void *pContext, int *aiCode);

				virtual int OrderReplay(OrderReplayInfo *pInfo, void *pContext, int *aiCode);

				virtual int PnlReplay(PnlReplayInfo *pInfo, void *pContext, int *aiCode);

				virtual int PnlUpdate(PnlInfo *pInfo, void *pContext, int *aiCode);

				virtual int PriceIncrUpdate(PriceIncrInfo *pInfo, void *pContext, int *aiCode);

				/*   ----------------------------------------------------------------   */

				virtual int BustReport(OrderBustReport *pReport, void *pContext, int *aiCode);

				virtual int CancelReport(OrderCancelReport *pReport, void *pContext, int *aiCode);

				virtual int FailureReport(OrderFailureReport *pReport, void *pContext, int *aiCode);

				virtual int FillReport(OrderFillReport *pReport, void *pContext, int *aiCode);

				virtual int ModifyReport(OrderModifyReport *pReport, void *pContext, int *aiCode);

				virtual int NotCancelledReport(OrderNotCancelledReport *pReport, void *pContext, int *aiCode);

				virtual int NotModifiedReport(OrderNotModifiedReport *pReport, void *pContext, int *aiCode);

				virtual int RejectReport(OrderRejectReport *pReport, void *pContext, int *aiCode);

				virtual int StatusReport(OrderStatusReport *pReport, void *pContext, int *aiCode);

				virtual int TradeCorrectReport(OrderTradeCorrectReport *pReport, void *pContext, int *aiCode);

				virtual int TriggerPulledReport(OrderTriggerPulledReport *pReport, void *pContext, int *aiCode);

				virtual int TriggerReport(OrderTriggerReport *pReport, void *pContext, int *aiCode);

				virtual int OtherReport(RApi::OrderReport *pReport, void *pContext, int *aiCode);

				/*   ----------------------------------------------------------------   */

				virtual int SodUpdate(SodReport *pReport, void *pContext, int *aiCode);

				/*   ----------------------------------------------------------------   */

				void SendOrder (OrderParams* pOrderParams);

				void CancelOrder (RApi::AccountInfo* pAccount, int orderNr);
				
				void CancelOrders (RApi::AccountInfo* pAccount);

				void GetAccounts ();

				void GetAccountOrders (RApi::AccountInfo *pAccount);

				void ReplayExecutions (RApi::AccountInfo *pAccount, int iStartSsboe, int iEndSsboe);

				void ReplayOrders (RApi::AccountInfo *pAccount, int iStartSsboe, int iEndSsboe);

				void SubscribeInstrument (tsNCharcb* pExchange, tsNCharcb* pActivityer, int iFlags);

				void UnsubscribeInstrument (tsNCharcb* pExchange, tsNCharcb* pActivityer);

				void SubscribeExchange (tsNCharcb* pExchange, int iFlags);

				void UnsubscribeExchange (tsNCharcb* pExchange);

				void SuspendInput ();

				void ResumeInput ();

				void GetReferenceData (tsNCharcb* symbol, tsNCharcb* exchange);

				bool IsConnected ();

				private:

				void UpdateConnectionStatus();

				private:
				gcroot<RithmicConnector^> pManaged;
				REngine *pREngine;
				//AdminCallback *pAdminCallback;

				int connectionCount;

				public:
				// This dictionary maps string status codes from the exchange to our own code scheme.
				gcroot<System::Collections::Generic::Dictionary<System::String^,Tradex::Market::MarketModes>^> pMarketModeMap;

				gcroot<System::Collections::Generic::Dictionary<System::String^,int>^> pBuySellTypeMap;

				// This map contains the COMPLETION_REASON codes.
				gcroot<System::Collections::Generic::Dictionary<System::String^,Tradex::Trading::OrderStatus>^> pCompletionReasonMap;

				gcroot<System::Collections::Generic::Dictionary<System::String^,int>^> pOrderDurationMap;

				gcroot<System::Collections::Generic::Dictionary<System::String^,int>^> pTriggerPriceTypeMap;

				// This maps the LINE_STATUS codes from Rapi.h to OrderStatus codes that Tradex uses.
				gcroot<System::Collections::Generic::Dictionary<System::String^,Tradex::Trading::OrderStatus>^> pLineStatusMap;

			};

		}
	}
}